package qq.service;

import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.MsgSender;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class TuPianJiService {
    public static ArrayList<String> sendTuPianJi(){
        ArrayList<String> strings = new ArrayList<>();
        try {
            int random = (int)(Math.random()*18);
            int i=0;
            while (true) {
                i++;
                if (i!=random){
                    continue;
                }
                Document document = Jsoup.connect("https://www.tupianzj.com/meinv/xinggan/").get();
//            System.out.println(document);
                // 使用 css选择器 提取列表新闻 a 标签
                // <a href="https://voice.hupu.com/nba/2484553.html" target="_blank">霍华德：夏休期内曾节食30天，这考验了我的身心</a>
                Elements elements = document.select("#container > div > div > div:nth-child(4) > div > ul > li:nth-child("+i+") > a");

                for (Element element : elements) {
                    String d_url = "https://www.tupianzj.com/"+element.attr("href");
                    System.out.println(d_url+"  ddad");
                    // 获取详情页链接
                    try{
                        int j=1;
                        while(true){
                            StringBuilder stringBuilder = new StringBuilder(d_url);
                            j++;
                            stringBuilder.insert(stringBuilder.length()-5,"_"+j);
                            Document document1 = Jsoup.connect(stringBuilder.toString()).get();
                            Elements elements1 = document1.select("#bigpicimg");
                            for (Element element1 : elements1){
                                String url = element1.attr("src");
                                strings.add(url);
                                System.out.println(url);
                            }
                        }
                    }catch (Exception e){
                        System.out.println("一个图集结束");
                    }
                }
                break;
            }
        } catch(IOException e){
            e.printStackTrace();
        }
        System.out.println("over");
        return strings;
    }

    public static ArrayList<String> getDMUrls() throws IOException {
        int i=0;
        int random = (int) (Math.random()*18);
        ArrayList<String> urls = new ArrayList<>();
        Document document = Jsoup.connect("https://www.hmecy.com/category/cos/").get();
        Elements elements = document.getElementsByClass("link-block");
        for (Element element:elements){
            if (i!=random){
                i++;
                continue;
            }
            // 获取详情页链接
            String d_url = element.attr("href");


            Document document1 = Jsoup.connect(d_url).get();
//            System.out.println(document);
            // 使用 css选择器 提取列表新闻 a 标签
            // <a href="https://voice.hupu.com/nba/2484553.html" target="_blank">霍华德：夏休期内曾节食30天，这考验了我的身心</a>
            Element element1 = document1.getElementById("entry-content");
            Elements imgs = element1.getElementsByTag("img");
            for (Element img : imgs){
                String url = img.attr("src");
                urls.add(url);
                System.out.println(url);
            }
            return urls;
        }
        return urls;
    }
}
